package com.mingmen.canting.zhenban;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mingmen.canting.R;
import com.mingmen.canting.fragment.BaseFragment;
import com.mingmen.canting.model.ZhenBanLeftBean;
import com.mingmen.canting.zhenbanlibrary.CheckListener;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("ValidFragment")
public class ZhenBanDingDanFragment extends BaseFragment implements CheckListener, ZhenBanLeftAdapter.OnItemClickListener, View.OnClickListener {

    private RecyclerView rvSort;
    private ZhenBanLeftAdapter mSortAdapter;

    private ZhenBanMySortDetailFragment mSortDetailFragment;

    String getData;
    String token;

    int position;

    List<ZhenBanLeftBean.ChildrenBean> childrenBeanList = new ArrayList<>();
    ZhenBanLeftBean zhenBanLeftBean;
    Gson gson;
    ImageView img_shousuo;
    RelativeLayout layout_all;
    ImageView zhankai;
    Context mContext;

    private IntentFilter intentFilter;
    private MyBroadCast netWorkChangeReceiver;
    Intent intent;
    LinearLayout layout_list;
    LinearLayout layout_nodata;

    private class MyBroadCast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //系统服务类
            String data = intent.getStringExtra("data");
            Log.d("OnReceiveData", data);

            if (childrenBeanList.size() > 0) {
                childrenBeanList.clear();
            }
            zhenBanLeftBean = new ZhenBanLeftBean();
            zhenBanLeftBean = gson.fromJson(data, ZhenBanLeftBean.class);
            mSortAdapter = new ZhenBanLeftAdapter(getActivity(), childrenBeanList);
            rvSort.setAdapter(mSortAdapter);
//        childrenBeanList = zhenBanLeftBean.getChildren();

            childrenBeanList.addAll(zhenBanLeftBean.getChildren());
            Intent intent1;
            if (childrenBeanList.size() > 0) {

                intent1 = new Intent("canting.chushizhang.ZhenBanMySortDetailFragment");
                intent1.putExtra("pos", childrenBeanList.get(0).getCategoryId());
                getActivity().sendBroadcast(intent);
            } else {
                intent1 = new Intent("canting.chushizhang.ZhenBanMySortDetailFragment");
                intent1.putExtra("pos", "0");
                getActivity().sendBroadcast(intent);
            }


            Log.d("zhenBanLeftBean", childrenBeanList.size() + "");
            Log.d("childrenBeanList", childrenBeanList.size() + "");
            mSortAdapter.setDataList(childrenBeanList);
            mSortAdapter.setOnItemClickListener(new ZhenBanLeftAdapter.OnItemClickListener() {
                @Override
                public void onItemClickListener(View view, int pos, List<ZhenBanLeftBean.ChildrenBean> myLiveLists) {
                    mSortAdapter.setCheckedPosition(pos);
                    Intent intent = new Intent("canting.chushizhang.ZhenBanMySortDetailFragment");
                    intent.putExtra("pos", myLiveLists.get(pos).getCategoryId());
                    getActivity().sendBroadcast(intent);
                }
            });
        }
    }

    private void init() {

        intentFilter = new IntentFilter();
        intentFilter.addAction("com.mingmen.canting.zhenban.ZhenBanDingDanFragment");
        netWorkChangeReceiver = new MyBroadCast();
        //注册广播
        getActivity().registerReceiver(netWorkChangeReceiver, intentFilter);
    }


    @Override
    public View initView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fr_zhenban_dingdan, null);
        TextView card_title_tv = (TextView) view.findViewById(R.id.card_title_tv);

        card_title_tv.setText(getData);
        mContext = getActivity();
        init();
        layout_list = view.findViewById(R.id.layout_list);
        layout_nodata = view.findViewById(R.id.layout_nodata);
        gson = new Gson();
        rvSort = (RecyclerView) view.findViewById(R.id.rv_sort);
        rvSort.setLayoutManager(new LinearLayoutManager(getActivity()));
        img_shousuo = view.findViewById(R.id.img_shousuo);
        img_shousuo.setOnClickListener(this);
        layout_all = view.findViewById(R.id.layout_all);
        layout_all.setOnClickListener(this);
        zhankai = view.findViewById(R.id.zhankai);
        zhankai.setOnClickListener(this);

        Log.d("getDatagetData", getData);
        Log.d("onCreateView", "onCreateView");
        if (childrenBeanList.size() > 0) {
            childrenBeanList.clear();
        }

        zhenBanLeftBean = new ZhenBanLeftBean();
        zhenBanLeftBean = gson.fromJson(getData, ZhenBanLeftBean.class);
        mSortAdapter = new ZhenBanLeftAdapter(getActivity(), childrenBeanList);
        rvSort.setAdapter(mSortAdapter);

        if (zhenBanLeftBean.getChildren().size() == 0) {
            Intent intent1 = new Intent("canting.chushizhang.ZhenBanMySortDetailFragment");
            intent1.putExtra("pos", "0");
            getActivity().sendBroadcast(intent1);
        }

        childrenBeanList.addAll(zhenBanLeftBean.getChildren());
        Log.d("zhenBanLeftBean", childrenBeanList.size() + "");

        Log.d("childrenBeanList", childrenBeanList.size() + "");
        mSortAdapter.setDataList(childrenBeanList);
        mSortAdapter.setOnItemClickListener(this);
        mSortAdapter = new ZhenBanLeftAdapter(getActivity(), childrenBeanList);
        rvSort.setAdapter(mSortAdapter);
        mSortAdapter.setOnItemClickListener(this);
        Bundle bundle = new Bundle();

        if (childrenBeanList.size() == 0) {
            layout_list.setVisibility(View.GONE);
            layout_nodata.setVisibility(View.VISIBLE);
        } else {
            layout_list.setVisibility(View.VISIBLE);
            layout_nodata.setVisibility(View.GONE);
        }
        if (childrenBeanList.size() == 0) {
//            bundle.putString("right", "0");
        } else {
            FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            mSortDetailFragment = new ZhenBanMySortDetailFragment();
            bundle.putString("right", childrenBeanList.get(0).getCategoryId());
            mSortDetailFragment.setArguments(bundle);
            fragmentTransaction.add(R.id.lin_fragment, mSortDetailFragment);
            fragmentTransaction.commit();
        }
        return view;
    }


    @Override
    public void initData() {

    }


    public static ZhenBanDingDanFragment getInstance(String title) {
        ZhenBanDingDanFragment sf = new ZhenBanDingDanFragment();
        sf.getData = title;
        Log.d("DingDandkdkdkd", title);

        return sf;
    }


    String myreceiveposition;

    private void setChecked(int position, boolean isLeft) {
        Log.d("p-------->", String.valueOf(position));
        mSortAdapter.setCheckedPosition(position);
        myreceiveposition = String.valueOf(position);

    }


    @Override
    public void check(int position, boolean isScroll) {
        setChecked(position, isScroll);
    }


    @Override
    public void onItemClickListener(View view, int pos, List<ZhenBanLeftBean.ChildrenBean> myLiveLists) {
        mSortAdapter.setCheckedPosition(pos);
        Intent intent = new Intent("canting.chushizhang.ZhenBanMySortDetailFragment");
        intent.putExtra("pos", myLiveLists.get(pos).getCategoryId());
        getActivity().sendBroadcast(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_shousuo:
                rvSort.setVisibility(View.GONE);
                zhankai.setVisibility(View.VISIBLE);
                break;
            case R.id.layout_all:

                break;
            case R.id.zhankai:
                rvSort.setVisibility(View.VISIBLE);
                zhankai.setVisibility(View.GONE);
                break;
        }
    }
}